﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DiscoveryLogic.Analytics.DataObjects;
using DiscoveryLogic.Analytics.DataObjects.Networks;

namespace DiscoveryLogic.Analytics.Modules.Networks
{
    /// <summary>
    /// Interaction logic for NetworkData.xaml
    /// </summary>
    public partial class NetworkData : UserControl
    {
        #region events
        public delegate void OnEntitySelected(EntityBase entity);
        public event OnEntitySelected EntitySelected;

        public delegate void OnEntitiesSelected(List<EntityBase> entities);
        public event OnEntitiesSelected EntitiesSelected;
        #endregion
        #region fields
        private NetworkDataPresenter _Presenter;
        #endregion

        #region ctor
        public NetworkData()
        {
            InitializeComponent();
        }

        public NetworkData(NetworkDataPresenter presenter)
            : this()
        {
            this._Presenter = presenter;
            this._Presenter.View = this;
        }
        #endregion

        #region init
        public void PopulateNetworkEntities(Network network)
        {
            List<PersonDO> persons=new List<PersonDO>();
            List<GrantDO> grants=new List<GrantDO>();
            List<ExternalOrgDO> orgs=new List<ExternalOrgDO>();
            List<PublicationDO> pubs=new List<PublicationDO>();
            List<Document> docs=new List<Document>();
            List<Subject> subjs=new List<Subject>();
            List<MiscEntity> miscs=new List<MiscEntity>();
            foreach(EntityBase entity in network.Records)
            {
                if(entity is PersonDO)
                    persons.Add(entity as PersonDO);
                else if(entity is GrantDO)
                    grants.Add(entity as GrantDO);
                else if(entity is ExternalOrgDO)
                    orgs.Add(entity as ExternalOrgDO);
                else if(entity is PublicationDO)
                    pubs.Add(entity as PublicationDO);
                else if(entity is Document)
                    docs.Add(entity as Document);
                else if(entity is Subject)
                    subjs.Add(entity as Subject);
                else if(entity is MiscEntity)
                    miscs.Add(entity as MiscEntity);
            }

            if(persons.Count>0)
            {
                this.borderPersons.Visibility = Visibility.Visible;
                this.dgPersons.ItemsSource = persons;
            }
            else
                this.borderPersons.Visibility = Visibility.Collapsed;
            if(grants.Count>0)
            {
                this.borderGrants.Visibility = Visibility.Visible;
                this.dgGrants.ItemsSource = grants;
            }
            else
                this.borderGrants.Visibility = Visibility.Collapsed;
            if(orgs.Count>0)
            {
                this.borderOrgs.Visibility = Visibility.Visible;
                this.dgOrgs.ItemsSource = orgs;
            }
            else
                this.borderOrgs.Visibility = Visibility.Collapsed;
            if(pubs.Count>0)
            {
                this.borderPubs.Visibility = Visibility.Visible;
                this.dgPubs.ItemsSource = pubs;
            }
            else
                borderPubs.Visibility = Visibility.Collapsed;
            if (docs.Count > 0)
            {
                this.borderDocs.Visibility = Visibility.Visible;
                this.dgDocs.ItemsSource = docs;
            }
            else
                borderDocs.Visibility = Visibility.Collapsed;
            if(subjs.Count>0)
            {
                this.borderSubjs.Visibility = Visibility.Visible;
                this.dgSubjs.ItemsSource = subjs;
            }
            else
                this.borderSubjs.Visibility = System.Windows.Visibility.Collapsed;

            if(miscs.Count>0)
            {
                this.borderMiscs.Visibility = System.Windows.Visibility.Visible;
                this.dgMiscs.ItemsSource = miscs;
            }
            else
                this.borderMiscs.Visibility = System.Windows.Visibility.Collapsed;

            if(network.Links !=null && network.Links.Count>0)
            {
                this.borderLinks.Visibility = Visibility.Visible;
                this.dgLinks.ItemsSource = network.GetLinksAsTable().DefaultView;
            }
            else
                this.borderLinks.Visibility = Visibility.Collapsed;
        }
        #endregion

        #region selection
        public void SelectEntity(EntityBase entity)
        {
            
        }
        #endregion

        #region subscribe events
        public void SubscribeViewDataEventHandler(Network network)
        {
            this._Presenter.RegisterViewDataEventHandler(network);
        }

        public void SubscribeEventHandler(EntityBase entity)
        {
            this._Presenter.UpdateViewSelection(entity);
        }
        #endregion

        private void dgPersons_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            List<EntityBase> selectedEntities = new List<EntityBase>();
            if(this.dgPersons.SelectedItems!=null && this.dgPersons.SelectedItems.Count>0)
            {
                foreach(object item in this.dgPersons.SelectedItems)
                {
                    if(item is PersonDO)
                    {
                        selectedEntities.Add(item as PersonDO);
                    }
                }

                if (this.EntitiesSelected != null)
                    this.EntitiesSelected(selectedEntities);
            }

        }

        private void dgGrants_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            List<EntityBase> selectedEntities = new List<EntityBase>();
            if (this.dgGrants.SelectedItems != null && this.dgGrants.SelectedItems.Count > 0)
            {
                foreach (object item in this.dgGrants.SelectedItems)
                {
                    if (item is GrantDO)
                    {
                        selectedEntities.Add(item as GrantDO);
                    }
                }

                if (this.EntitiesSelected != null)
                    this.EntitiesSelected(selectedEntities);
            }
        }

        private void dgPubs_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            List<EntityBase> selectedEntities = new List<EntityBase>();
            if (this.dgPubs.SelectedItems != null && this.dgPubs.SelectedItems.Count > 0)
            {
                foreach (object item in this.dgPubs.SelectedItems)
                {
                    if (item is PublicationDO)
                    {
                        selectedEntities.Add(item as PublicationDO);
                    }
                }

                if (this.EntitiesSelected != null)
                    this.EntitiesSelected(selectedEntities);
            }
        }

        private void dgOrgs_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            List<EntityBase> selectedEntities = new List<EntityBase>();
            if (this.dgOrgs.SelectedItems != null && this.dgOrgs.SelectedItems.Count > 0)
            {
                foreach (object item in this.dgOrgs.SelectedItems)
                {
                    if (item is ExternalOrgDO)
                    {
                        selectedEntities.Add(item as ExternalOrgDO);
                    }
                }

                if (this.EntitiesSelected != null)
                    this.EntitiesSelected(selectedEntities);
            }
        }

        private void dgDocs_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            List<EntityBase> selectedEntities = new List<EntityBase>();
            if (this.dgDocs.SelectedItems != null && this.dgDocs.SelectedItems.Count > 0)
            {
                foreach (object item in this.dgDocs.SelectedItems)
                {
                    if (item is Document)
                    {
                        selectedEntities.Add(item as Document);
                    }
                }

                if (this.EntitiesSelected != null)
                    this.EntitiesSelected(selectedEntities);
            }
        }

        private void dgSubjs_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            List<EntityBase> selectedEntities = new List<EntityBase>();
            if (this.dgSubjs.SelectedItems != null && this.dgSubjs.SelectedItems.Count > 0)
            {
                foreach (object item in this.dgSubjs.SelectedItems)
                {
                    if (item is Subject)
                    {
                        selectedEntities.Add(item as Subject);
                    }
                }

                if (this.EntitiesSelected != null)
                    this.EntitiesSelected(selectedEntities);
            }
        }

        private void dgMiscs_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            List<EntityBase> selectedEntities = new List<EntityBase>();
            if (this.dgMiscs.SelectedItems != null && this.dgMiscs.SelectedItems.Count > 0)
            {
                foreach (object item in this.dgMiscs.SelectedItems)
                {
                    if (item is MiscEntity)
                    {
                        selectedEntities.Add(item as MiscEntity);
                    }
                }

                if (this.EntitiesSelected != null)
                    this.EntitiesSelected(selectedEntities);
            }
        }
    }
}
